namespace WebSMS;

[Route("[controller]/[action]")]
[Controller]
[Authorize]
public class PostsController : ControllerBase
{
    private readonly PostService _service;
    public PostsController(PostService service)
    {
        _service = service;
    }

    [HttpGet]
    public ResponseObject<PagedList<PostPo>> GetPostAll(int page, int size)
    {
        var pagedList = _service.GetPostAll(page, size);
        return new ResponseObject<PagedList<PostPo>>().Ok(pagedList,"查询成功！");
    }

    [HttpGet]
    public ResponseObject<PagedList<PostPo>> GetPostByDepartment(Guid department)
    {
       var pageList = _service.GetAllPostByDepartmentId(department);
       return new ResponseObject<PagedList<PostPo>>().Ok(pageList,"查询成功！");
    }
    
    [HttpPost]
    public async Task<ResponseObject<bool>> AddPost([FromBody] PostForm post)
    {
        var status = await _service.AddPost(new PostPo()
        {
            Name = post.Name,
            Description = post.Description,
            DepartmentId = post.DepartmentId,
            Id = Guid.NewGuid()
        });
        return new ResponseObject<bool>().Ok(status,"添加成功！");
    }

    [HttpPut]
    public async Task<ResponseObject<bool>> UpdatePost([FromBody] PostPo post)
    {
      return new ResponseObject<bool>().Ok(await _service.UpdatePost(post),"更新成功！");
    }

    [HttpDelete]
    public async Task<ResponseObject<bool>> DeletePost(Guid id)
    {
        bool deletePost = await _service.DeletePost(id);
        if (!deletePost)
        {
            return new ResponseObject<bool>().Ok("删除成功！");
        }

        return new ResponseObject<bool>().Fail("删除失败，当前工作岗位中存在员工！");
    }


    [HttpGet]
    public async Task<ResponseObject<object>> GetPostDetails(Guid id)
    {
        var details = await _service.GetPostDetails(id);
        return new ResponseObject<object>().Ok(details,"查询成功！");
    }
}